package com.jinlejia.erp.ui.house;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ActivityHouseCoreInfoBinding;
import com.jinlejia.erp.utils.CallUtils;
import com.weight.CustomToast;

public class HouseCoreInfoActivity extends AppCompatActivity {

    ClipboardManager cmb;

    private ActivityHouseCoreInfoBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_house_core_info);
        binding.telNumber.setOnLongClickListener(this::onViewLongClicked);
        binding.backupTelNumber.setOnLongClickListener(this::onViewLongClicked);
        binding.linkerTelNumber.setOnLongClickListener(this::onViewLongClicked);

        binding.close.setOnClickListener(this::onViewClicked);
        binding.telNumber.setOnClickListener(this::onViewClicked);
        binding.backupTelNumber.setOnClickListener(this::onViewClicked);
        binding.linkerTelNumber.setOnClickListener(this::onViewClicked);
        binding.callBtn.setOnClickListener(this::onViewClicked);

        cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        JSONObject data = JSON.parseObject(getIntent().getStringExtra("content"));
        setData(data);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        JSONObject data = JSON.parseObject(intent.getStringExtra("content"));
        setData(data);
    }

    private void setData(JSONObject data) {
        binding.houseName.setText(data.getString("estate").concat(data.getString("address")));
        binding.owerName.setText(data.getString("owner"));
        binding.telNumber.setText(data.getString("tel"));
        //业主备用电话
        if (data.containsKey("spareTel") && !TextUtils.isEmpty(data.getString("spareTel"))) {
            binding.backupTelNumber.setText(data.getString("spareTel"));
        } else {
            binding.backupTelNumber.setVisibility(View.GONE);
        }
        //业主联系人
        if (data.containsKey("contact") && !TextUtils.isEmpty(data.getString("contact"))) {
            binding.linkerName.setText(data.getString("contact"));
            binding.linkerTelNumber.setText(data.getString("contactTel"));
        } else {
            binding.linkerName.setVisibility(View.GONE);
            binding.linkerTelNumber.setVisibility(View.GONE);
        }
    }


    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.tel_number:
            case R.id.call_btn:
                CallUtils.CallTel(HouseCoreInfoActivity.this, binding.telNumber.getText().toString());
                break;
            case R.id.backup_tel_number:
                CallUtils.CallTel(HouseCoreInfoActivity.this, binding.backupTelNumber.getText().toString());
                break;
            case R.id.linker_tel_number:
                CallUtils.CallTel(HouseCoreInfoActivity.this, binding.linkerTelNumber.getText().toString());
                break;
            default:
                break;
        }
    }

    public boolean onViewLongClicked(View view) {

        switch (view.getId()) {
            case R.id.tel_number:
                ClipData clip = ClipData.newPlainText("tel", binding.telNumber.getText());
                cmb.setPrimaryClip(clip);
                CustomToast.showToast(HouseCoreInfoActivity.this, "已复制号码，请在拨号页面粘贴拨打", 1500);
                break;
            case R.id.backup_tel_number:
                ClipData clip1 = ClipData.newPlainText("tel", binding.backupTelNumber.getText());
                cmb.setPrimaryClip(clip1);
                CustomToast.showToast(HouseCoreInfoActivity.this, "已复制号码，请在拨号页面粘贴拨打", 1500);
                break;
            case R.id.linker_tel_number:
                ClipData clip2 = ClipData.newPlainText("tel", binding.linkerTelNumber.getText());
                cmb.setPrimaryClip(clip2);
                CustomToast.showToast(HouseCoreInfoActivity.this, "已复制号码，请在拨号页面粘贴拨打", 1500);
                break;
            default:
                break;
        }

        return true;
    }
}
